#coding=utf-8
import unittest
from appium import webdriver
from selenium import webdriver
from time import sleep
from nav_plugin_op import nav_plugin_factory
from Tool.usefulTools import userfulToolsFactory
from selenium.common.exceptions import WebDriverException

navOpenFactory = nav_plugin_factory()
userfulTool = userfulToolsFactory()


nav_name = "工作"
plugin_name = "问题上报"
add_button_id = "cn.com.egova.pluginreport:id/report_new" #新增按钮ID
eventtype_list_id = "cn.com.egova.pluginreport:id/report_eventtype" #问题类型列表控件
maintype_list_id = "cn.com.egova.pluginreport:id/report_maintype" #大类列表控件
subtype_list_id = "cn.com.egova.pluginreport:id/report_subtype" #小类列表控件
eventtype_name = "部件"
maintype_name = "市容环境"
subtype_name = "果皮箱"
description_id = "cn.com.egova.pluginreport:id/report_desc"
description_text = "客户端自动化测试上报...."
location_report_id = "cn.com.egova.pluginreport:id/location_desc"
map_confirm_id = "cn.com.egova.pluginmap:id/map_bottom_menu"
ok_button_id = "android:id/button1"
cancel_button_id = ""
media_view_id = "cn.com.egova.pluginreport:id/media_view" #上传多媒体的+ID
tool_photoadd_id = "cn.com.egova.pluginreport:id/tool_photoadd" #上传多媒体的---相册 ID

class rec_report_factory():

    def rec_report_excute(self,driver):

        try:
            # 点击工作的导航，打开工作列表
            nav_plugin_factory.nav_open(navOpenFactory,nav_name.decode("utf-8"),driver)
            # 点击"问题上报"的插件，打开插件
            nav_plugin_factory.plugin_open(navOpenFactory,plugin_name.decode("utf-8"),driver)
            #driver.find_element_by_name("问题上报".decode("utf-8")).click()
            # 点击 新增按钮，打开新增界面
            driver.find_element_by_id(add_button_id).click()
            # 点击"问题类型"的控件，打开下拉
            driver.find_element_by_id(eventtype_list_id).click()
            sleep(1)
            # 选择类别为：部件
            driver.find_element_by_name(eventtype_name.decode("utf-8")).click()
            sleep(1)
            # 点击"大类"输入框，打开下拉列表
            driver.find_element_by_id(maintype_list_id).click()
            sleep(1)
            # 选择"大类"别为：市容环境
            driver.find_element_by_name(maintype_name.decode("utf-8")).click()
            sleep(2)
            # 点击"小类"输入框，打开下拉列表
            driver.find_element_by_id(subtype_list_id).click()
            sleep(1)
            # 选择"小类"别为：果皮箱
            driver.find_element_by_name(subtype_name.decode("utf-8")).click()
            sleep(2)
            # 输入“描述”内容
            driver.find_element_by_id(description_id).send_keys(description_text.decode("utf-8"))
            sleep(1)
            # 选择"上报界面的位置定位按钮"
            driver.find_element_by_id(location_report_id).click()
            sleep(2)
            # 点击"选定位置按钮"并确认
            driver.find_element_by_id(map_confirm_id).click()
            sleep(2)
            driver.find_element_by_id(ok_button_id).click()
            sleep(2)
            # 从相册选取照片
            driver.find_element_by_id(media_view_id).click()
            driver.find_element_by_id(tool_photoadd_id).click()
            sleep(2)
            driver.find_element_by_xpath(
                "//android.widget.RelativeLayout/android.widget.GridView/android.widget.RelativeLayout[1]/android.widget.ImageButton").click()
            driver.find_element_by_id("cn.com.egova.pluginreport:id/id_select").click()
            sleep(1)
            # 进行提交并确认
            driver.find_element_by_id("cn.com.egova.pluginreport:id/btn_report").click()
            driver.find_element_by_id("android:id/button1").click()
            sleep(3)
            # 判断是否上报成功，并进行截图
            ReportResult = driver.find_element_by_xpath(
                "//android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.ScrollView/android.widget.TextView").get_attribute(
                "text")
            if userfulToolsFactory.test_check(userfulTool, "test", ReportResult):
                print "report successful...."
                driver.get_screenshot_as_file("d:/test.png")
            else:
                print "report failed...."
                print ReportResult
                driver.get_screenshot_as_file("d:/test.png")
            # 关闭上报弹窗，并返回工作列表
            driver.find_element_by_id("android:id/button1").click()
            sleep(2)
            driver.find_element_by_id("android:id/up").click()
        except Exception,error:
            print error

    def rec_report_open_plug(self,driver,file_path):

        try:
            # 点击工作的导航，打开工作列表
            nav_plugin_factory.nav_open(navOpenFactory,nav_name.decode("utf-8"),driver)
            # 点击"问题上报"的插件，打开插件
            nav_plugin_factory.plugin_open(navOpenFactory,plugin_name.decode("utf-8"),driver)

            if driver.find_element_by_id(add_button_id):
                driver.get_screenshot_as_file("d:/test.png")

        except WebDriverException,error:
            print error
            driver.get_screenshot_as_file("d:/error.png")
            raise

    def rec_report_open_NewReportUI(self,driver):

        try:
            # 点击 新增按钮，打开新增界面
            print u'打开新增界面'
            driver.find_element_by_id(add_button_id).click()

            if driver.find_element_by_id(eventtype_list_id).is_displayed():
                driver.get_screenshot_as_file("d:/test.png")

        except WebDriverException,error:
            print error
            driver.get_screenshot_as_file("d:/error.png")
            raise

    def rec_report_open_plug2(self,driver,file_path):


        try:
            # 点击工作的导航，打开工作列表
            nav_plugin_factory.nav_open(navOpenFactory,nav_name.decode("utf-8"),driver)
            # 点击"问题上报"的插件，打开插件
            nav_plugin_factory.plugin_open(navOpenFactory,plugin_name.decode("utf-8"),driver)

            if driver.find_element_by_id(add_button_id):
                driver.get_screenshot_as_file("d:/test.png")

        except WebDriverException,error:
            print error
            driver.get_screenshot_as_file("d:/error.png")
            raise

        print "test the html report"

if __name__ == '__main__':
    unittest.main()
